#!/bin/bash

#*******************************************************************************
#
# build.sh - Run a complete build which converts the latex source to pdf, odt and html.
#
# Copyright (c) 2009 Philip Wenig
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
# Contributors:
#	Philip Wenig
#
#*******************************************************************************/

#
# Remove the old build.log file.
#
echo '---------------' >> build.log
rm -f build.log
echo 'remove old build.log ... done' >> build.log
echo '---------------' >> build.log

#
# Converts all eps files in figures to pdf with epstopdf.
#
echo 'run eps conversion to pdf ...' >> build.log
for file in $(find ./../include/figures/ -name '*.eps'); do
	echo 'convert file '$file >> build.log
	epstopdf $file
	echo 'done' >> build.log	
done
echo '---------------' >> build.log

#
# Run the initial latex compilation.
#
echo 'run first compilation ...' >> build.log
latex -interaction=nonstopmode main.tex
echo 'done' >> build.log

#
# Compile the document 3 times to include abbreviations, index and bibliography correctly.
#
echo 'compile abbreviations, index, bibliography 3x ...' >> build.log
run=1
while [ $run -le 3 ]; do
	# abbreviations
	echo 'run abbreviations' >> build.log		
	makeindex main.nlo -s nomencl.ist -o main.nls
	echo 'abbreviations done run: '$run >> build.log
	# index
	echo 'run index' >> build.log	
	makeindex main.idx -o main.ind
	echo 'index done run: '$run >> build.log	
	# bibliography
	echo 'run bibliography' >> build.log	
	bibtex main.aux
	echo 'bibliography done run: '$run >> build.log
	
	run=$[run+1]
done
echo '---------------' >> build.log

#
# Create the dvi file.
#
echo 'run second compilation ...' >> build.log	
latex -interaction=nonstopmode main.tex
echo 'done' >> build.log
echo '---------------' >> build.log

#
# Converts eps to pdf and creates the pdf file.
#
echo 'run pdf compilation ...' >> build.log
# TODO MikTeX has another command, not shell-escape.
pdflatex -shell-escape -interaction=nonstopmode main.tex
echo 'done' >> build.log
echo '---------------' >> build.log

#
# Converts the document to a odf file.
# Could be also htlatex 'main.tex' 'xhtml,ooffice' 'ooffice/! -cmozhtf' '-coo' '-cvalidate'
# 
echo 'run odt compilation ...' >> build.log	
mk4ht 'oolatex' 'main.tex'
#htlatex 'main.tex' 'xhtml,ooffice' 'ooffice/! -cmozhtf' '-coo' '-cvalidate'
echo 'done' >> build.log
echo '---------------' >> build.log

#
# Converts the latex document to html.
#
echo 'run html compilation ...' >> build.log	
htlatex 'main.tex' 'html,info'
echo 'done' >> build.log
echo '---------------' >> build.log

#
# Everythings has been compiled correctly.
#
echo 'Congratulations, no problems have been occured' >> build.log
